package librus.cheeseChaser.components
{
	import librus.cheeseChaser.events.MoveEvent;
	
	import flash.events.Event;
	import flash.geom.Point;
	
	import starling.events.EventDispatcher;
	
	import librus.cheeseChaser.tile.Move;
	import librus.cheeseChaser.tile.Tile;
	import librus.cheeseChaser.tile.TileModel;
	
	import librus.cheeseChaser.util.Assets;
	import librus.cheeseChaser.util.Direction;
	import librus.cheeseChaser.util.Util;

	public class MatControl extends EventDispatcher
	{		
		public function MatControl(mat:Mat)
		{
			this.mat = mat;
			positionMap = {};
			viewMap = {};
			list = [];
		}
		
		private var list:Array;
		private var positionMap:Object;
		private var viewMap:Object;
		private var mat:Mat;
		private var actualPoints:int;
		
		public function addTile(tile:Tile, position:Point):void
		{
			var item:TileModel = new TileModel(tile);
				item.position = position;
				
			positionMap[Util.pos2Str(position)] = item;
			viewMap[tile] = item;
			list.push(item);
		}
		
		public function drawPossibleMoves(item:Tile):void
		{
			var move:Move;
			var position:Point
			var tileSize:int = Assets.TILE_SIZE;
			var moves:Array = getPossibleMoves(item);
			var center:Point;
			
			for (var i:int=0; i< moves.length;i++)
			{
				center = mat.centerTilePosition;
				position = moves[i] as Point;
				move = new Move;
				move.x = center.x + position.x * tileSize;
				move.y = center.y + position.y * tileSize;	
				move.position = position;
				move.addEventListener(MoveEvent.MOVE_SELECTED, onMoveSelected);
				mat.addMove(move);
			}
		}
		
		private function onMoveSelected(event:MoveEvent):void
		{			
			var center:Point = mat.centerTilePosition;
			var tileSize:int = Assets.TILE_SIZE;
			
			var position:Point = new Point();
				position.x = center.x + event.modelPosition.x * tileSize;
				position.y = center.y + event.modelPosition.y * tileSize;
				
			mat.removeAllMoves();
			dispatchEvent(new MoveEvent(MoveEvent.MOVE_SELECTED,event.modelPosition, position));
		}
		
		protected function getPossibleMoves(tile:Tile):Array
		{
			var pos:Point;
			var result:Array = [];
			var direction:Direction;
			var directions:Array = Direction.ALL_DIRECTIONS;			
			var item:TileModel = viewMap[tile] as TileModel;
			var tmp:TileModel;
			
			for (var i:int=0; i< directions.length; i++)	
			{
				direction = directions[i];
				pos = item.position.add(direction.value);
				tmp = positionMap[Util.pos2Str(pos)] as TileModel;
				if (!tmp)
					result.push(pos);				
			}
			return result;
		}
	
		
		public function countPoints():void
		{
			var evaluator:PointEvaluator = new PointEvaluator(positionMap, list);
				evaluator.evaluate();
						
			actualPoints = evaluator.mousePoints + 
						   evaluator.cheesePoints +
						   evaluator.cheeseBonusPoints;
			
			if (evaluator.isOver)
			{
				actualPoints = 0;
				trace("GAME OVER");
			}
			
			trace(evaluator.isOver,evaluator.mousePoints,evaluator.cheesePoints, evaluator.cheeseBonusPoints, actualPoints);
		}
	}
}